package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.Machine;
import model.Order;
import model.Repair;
import model.Service;
import model.SparePart;
import model.StorageArea;

public class GuiDesignPlan extends JFrame {
	private boolean currentUser;
	private Font fontMain = new Font("Calibri", 0, 18);
	private JPanel pnlWest, pnlEast, pnlNorth, pnlSouth, pnlCenter;
	// South Panel components
	private JTextField txfSearch;
	private JButton btnSearch;
	private JComboBox comboSearch;
	private DefaultComboBoxModel comboModel;
	// north panel components
	private JPanel pnlNorthLeft, pnlNorthRight;
	private JButton btnLogOut;
	private JLabel lblImage, lblAccount;
	// center panel components
	private JPanel pnlCenterLeft, pnlCenterRight;
	private JList listMain;
	private JScrollPane scpMain;
	private DefaultListModel listModel;
	private JLabel lblList, lblDetails;
	// west panel components
	private JButton btnSparePart, btnMachine, btnRepair, btnOrder, btnDrawer;
	// east panel components
	private JButton btnCreate, btnUpdate, btnRemove, btnEnd, btnStatistics;
	// center right panel
	private JPanel pnlCenterRightUp, pnlCenterRightUp2, pnlCenterRightDown;
	private JList listSub, listSub2;
	private JScrollPane scpSub, scpSub2;
	private JLabel lblInfo1, lblInfo2, lblInfo3, lblInfo4, lblInfo5;
	private Service service = Service.getInstance();
    // menu stuff
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem mnuItem;
	
	private BtnController btnCtrl = new BtnController();
	private ListController listCtrl = new ListController();
	
	private JTextField txfInfo1, txfInfo2, txfInfo3, txfInfo4, txfInfo5;

	public GuiDesignPlan() {

		setTitle("GUI TryOut v1.2");
		setLocation(250, 100);
		setSize(900, 450);
		setMinimumSize(new Dimension(800, 400));
		setLayout(new BorderLayout(0, 0));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				arrangeButtons();
			}
		});
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				// if (GuiDesignPlan.this.isCurrentUser()) {
				// GuiDesignPlan.this.btnRepair.setEnabled(false);
				// }
				listMain.setListData(service.returnMachines().toArray());
				listMain.setSelectedIndex(0);
				lblList.setText("Machines: ");
			}
		});

		// creating the panels
		pnlWest = new JPanel(null);
		pnlWest.setPreferredSize(new Dimension(155, 350));
		pnlWest.setBorder(BorderFactory.createLineBorder(Color.black));
		add(pnlWest, BorderLayout.WEST);

		pnlNorth = new JPanel(new GridLayout(1, 2, 5, 0));
		add(pnlNorth, BorderLayout.NORTH);

		pnlEast = new JPanel(null);
		pnlEast.setPreferredSize(new Dimension(155, 350));
		pnlEast.setBorder(BorderFactory.createLineBorder(Color.black));
		add(pnlEast, BorderLayout.EAST);

		pnlSouth = new JPanel(new FlowLayout(1, 15, 5));
		pnlSouth.setPreferredSize(new Dimension(400, 50));
		add(pnlSouth, BorderLayout.SOUTH);

		pnlCenter = new JPanel(new GridLayout(0, 2, 5, 0));
		add(pnlCenter, BorderLayout.CENTER);

		pnlCenterLeft = new JPanel(new BorderLayout(0, 0));
		pnlCenterLeft.setBackground(Color.white);
		pnlCenter.add(pnlCenterLeft);

		pnlCenterRight = new JPanel(new GridLayout(2, 1, 0, 0));
		pnlCenterRight.setBackground(Color.white);
		pnlCenter.add(pnlCenterRight);

		// adding the simple elements
		// South Panel elements:
		txfSearch = new JTextField();
		txfSearch.setPreferredSize(new Dimension(200, 30));
		txfSearch.setFont(fontMain);
		btnSearch = new JButton("Search");
		btnSearch.setPreferredSize(new Dimension(120, 30));
		btnSearch.setToolTipText("Search for a specific item");
		btnSearch.setCursor(new Cursor(13));
		btnSearch.setFont(fontMain);
		comboModel = new DefaultComboBoxModel();
		comboSearch = new JComboBox(comboModel);
		comboSearch.setFont(fontMain);
		comboSearch.setToolTipText("Search for ...");
		comboSearch.setPreferredSize(new Dimension(120, 30));
		comboSearch.setCursor(new Cursor(13));
		comboModel.addElement("Machine"); // testing
		comboModel.addElement("Repair"); // testing
		comboModel.addElement("Spare Part"); // testing
		comboModel.addElement("Drawer"); // testing
		comboModel.addElement("Order"); // testing

		pnlSouth.add(comboSearch);
		pnlSouth.add(txfSearch);
		pnlSouth.add(btnSearch);

		// North Panel elements:
		pnlNorthLeft = new JPanel(new FlowLayout(0, 20, 0));
		pnlNorth.add(pnlNorthLeft);
		pnlNorthRight = new JPanel(new FlowLayout(2));
		pnlNorth.add(pnlNorthRight);

		lblImage = new JLabel("    Inventory Management System");
		lblImage.setIcon(new ImageIcon("gui/SiteLogo.png"));
		lblImage.setForeground(Color.red);
		lblImage.setFont(fontMain);
		pnlNorthLeft.add(lblImage);

		btnLogOut = new JButton("Log Out");
		btnLogOut.setPreferredSize(new Dimension(105, 30));
		btnLogOut.addActionListener(new BtnController());
		btnLogOut.setFont(fontMain);
		btnLogOut.setToolTipText("Log out of the system");
		btnLogOut.setCursor(new Cursor(13));
		pnlNorthRight.add(btnLogOut);

		// Center Panel elements
		listModel = new DefaultListModel();
		listMain = new JList(listModel);
		listMain.setFont(fontMain);
		listMain.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listMain.setSelectionBackground(Color.darkGray);
		listMain.addListSelectionListener(listCtrl);
		scpMain = new JScrollPane(listMain);
		pnlCenterLeft.add(scpMain);
		lblList = new JLabel("List");
		lblList.setFont(fontMain);
		lblList.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		pnlCenterLeft.add(lblList, BorderLayout.NORTH);
		lblDetails = new JLabel("Details: ");
		lblDetails.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		lblDetails.setFont(fontMain);

		// West Panel
		btnMachine = new JButton("Machine View");
		btnMachine.setSize(145, 30);
		btnMachine.setLocation(5, 5);
		btnMachine.setFont(fontMain);
		btnMachine.setToolTipText("View Machines");
		btnMachine.setCursor(new Cursor(13));
		pnlWest.add(btnMachine);

		btnRepair = new JButton("Repair View");
		btnRepair.setSize(145, 30);
		btnRepair.setLocation(5, 45);
		btnRepair.setFont(fontMain);
		btnRepair.setToolTipText("View Repairs");
		btnRepair.setCursor(new Cursor(13));
		pnlWest.add(btnRepair);

		btnSparePart = new JButton("SparePart View");
		btnSparePart.setSize(145, 30);
		btnSparePart.setLocation(5, 85);
		btnSparePart.setFont(fontMain);
		btnSparePart.setToolTipText("View Spareparts");
		btnSparePart.setCursor(new Cursor(13));
		pnlWest.add(btnSparePart);

		btnDrawer = new JButton("Drawer View");
		btnDrawer.setSize(145, 30);
		btnDrawer.setLocation(5, 125);
		btnDrawer.setFont(fontMain);
		btnDrawer.setToolTipText("View Drawers");
		btnDrawer.setCursor(new Cursor(13));
		pnlWest.add(btnDrawer);

		btnOrder = new JButton("Order View");
		btnOrder.setSize(145, 30);
		btnOrder.setLocation(5, 165);
		btnOrder.setFont(fontMain);
		btnOrder.setToolTipText("View Orders");
		btnOrder.setCursor(new Cursor(13));
		pnlWest.add(btnOrder);

		// East Panel
		btnCreate = new JButton("New");
		btnCreate.setSize(145, 30);
		btnCreate.setLocation(5, 5);
		btnCreate.setFont(fontMain);
		btnCreate.setToolTipText("Create a new item of the selected type");
		btnCreate.setCursor(new Cursor(13));
		pnlEast.add(btnCreate);

		btnUpdate = new JButton("Update");
		btnUpdate.setSize(145, 30);
		btnUpdate.setLocation(5, 5);
		btnUpdate.setFont(fontMain);
		btnUpdate.setToolTipText("Update info for the selected item");
		btnUpdate.setCursor(new Cursor(13));
		pnlEast.add(btnUpdate);

		btnRemove = new JButton("Delete");
		btnRemove.setSize(145, 30);
		btnRemove.setLocation(5, 5);
		btnRemove.setFont(fontMain);
		btnRemove.setToolTipText("Delete the selected item");
		btnRemove.setCursor(new Cursor(13));
		pnlEast.add(btnRemove);

		btnEnd = new JButton("End");
		btnEnd.setSize(145, 30);
		btnEnd.setLocation(5, 5);
		btnEnd.setFont(fontMain);
		btnEnd.setToolTipText("End");
		btnEnd.setCursor(new Cursor(13));
		pnlEast.add(btnEnd);

		btnStatistics = new JButton("Statistics");
		btnStatistics.setSize(145, 30);
		btnStatistics.setLocation(5, 5);
		btnStatistics.setFont(fontMain);
		btnStatistics.setToolTipText("View Statistics");
		btnStatistics.setCursor(new Cursor(13));
		pnlEast.add(btnStatistics);

		btnMachine.addActionListener(btnCtrl);
		btnRepair.addActionListener(btnCtrl);
		btnSparePart.addActionListener(btnCtrl);
		btnOrder.addActionListener(btnCtrl);
		btnDrawer.addActionListener(btnCtrl);
		btnCreate.addActionListener(btnCtrl);
		btnUpdate.addActionListener(btnCtrl);
		btnRemove.addActionListener(btnCtrl);
		btnEnd.addActionListener(btnCtrl);
		btnStatistics.addActionListener(btnCtrl);
		// panel center right
		pnlCenterRightUp = new JPanel(new GridLayout(5, 1, 0, 0));
		pnlCenterRightUp2 = new JPanel(new BorderLayout(0, 0));
	//	pnlCenterRightUp.setBorder(BorderFactory.createLineBorder(Color.black));
	//	pnlCenterRightUp2.setBorder(BorderFactory.createLineBorder(Color.black));
		pnlCenterRight.setBorder(BorderFactory.createLineBorder(Color.black));
		pnlCenterLeft.setBorder(BorderFactory.createLineBorder(Color.black));

		listSub = new JList();
		listSub.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listSub.setSelectionBackground(Color.darkGray);
		listSub.setFont(fontMain);
		listSub.addListSelectionListener(listCtrl);
		scpSub = new JScrollPane(listSub);

		pnlCenterRightDown = new JPanel(new GridLayout(1, 2, 1, 1));
		pnlCenterRight.add(pnlCenterRightDown);

		listSub2 = new JList();
		listSub2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listSub2.setSelectionBackground(Color.darkGray);
		listSub2.setFont(fontMain);
		listSub2.addListSelectionListener(listCtrl);
		listSub2.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		listSub.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		listMain.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		scpSub2 = new JScrollPane(listSub2);

		txfInfo1 = new JTextField("");
		txfInfo1.setFont(fontMain);
		txfInfo1.setEditable(false);
		txfInfo1.setBorder(null);
		txfInfo2 = new JTextField("");
		txfInfo2.setFont(fontMain);
		txfInfo2.setEditable(false);
		txfInfo2.setBorder(null);
		txfInfo3 = new JTextField("");
		txfInfo3.setFont(fontMain);
		txfInfo3.setEditable(false);
		txfInfo3.setBorder(null);
		txfInfo4 = new JTextField("");
		txfInfo4.setFont(fontMain);
		txfInfo4.setEditable(false);
		txfInfo4.setBorder(null);
		txfInfo5 = new JTextField("");
		txfInfo5.setFont(fontMain);
		txfInfo5.setEditable(false);
		txfInfo5.setBorder(null);

		pnlCenterRightUp2.add(lblDetails, BorderLayout.NORTH);
		pnlCenterRightUp.add(txfInfo1);
		pnlCenterRightUp.add(txfInfo2);
		pnlCenterRightUp.add(txfInfo3);
		pnlCenterRightUp.add(txfInfo4);
		pnlCenterRightUp.add(txfInfo5);
		pnlCenterRightUp.setCursor(new Cursor(2));

		pnlCenterRight.add(pnlCenterRightUp2);
		pnlCenterRightUp2.add(pnlCenterRightUp, BorderLayout.CENTER);
		pnlCenterRightUp2.setBackground(Color.white);
		pnlCenterRight.add(pnlCenterRightDown);
		pnlCenterRightDown.add(scpSub);
		pnlCenterRightDown.add(scpSub2);
		
		//menu components
		menuBar= new JMenuBar();
		setJMenuBar(menuBar);
		menu = new JMenu("File");
		menuBar.add(menu);
		mnuItem = new JMenuItem("Exit");
		menu.add(mnuItem);
		mnuItem.addActionListener(btnCtrl);
		
	}

	public void arrangeButtons() {
		btnMachine.setLocation(5, pnlWest.getHeight() / 10);
		btnRepair.setLocation(5, 2 * pnlWest.getHeight() / 10 + 20);
		btnSparePart.setLocation(5, 3 * pnlWest.getHeight() / 10 + 40);
		btnDrawer.setLocation(5, 4 * pnlWest.getHeight() / 10 + 60);
		btnOrder.setLocation(5, 5 * pnlWest.getHeight() / 10 + 80);

		btnCreate.setLocation(5, pnlEast.getHeight() / 10);
		btnUpdate.setLocation(5, 2 * pnlEast.getHeight() / 10 + 20);
		btnRemove.setLocation(5, 3 * pnlEast.getHeight() / 10 + 40);
		btnEnd.setLocation(5, 4 * pnlEast.getHeight() / 10 + 60);
		btnStatistics.setLocation(5, 5 * pnlEast.getHeight() / 10 + 80);
	}

	public boolean isCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(boolean currentUser) {
		this.currentUser = currentUser;
	}

	public class BtnController implements ActionListener {
		// Service service = Service.getInstance();

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnLogOut) {
				GuiDesignPlan.this.dispose();
				LogON login = new LogON();
				login.setVisible(true);
			}
			if (e.getSource() == btnMachine) {
				listModel.clear();
				for (Machine m : service.returnMachines()) {
					listModel.addElement(m);
				}
				listMain.setModel(listModel);
				listMain.setSelectedIndex(0);
				lblList.setText("Machines: ");
			}
			if (e.getSource() == btnSparePart) {
				listModel.clear();
				for (SparePart sp : service.returnParts()) {
					listModel.addElement(sp);
				}
				listMain.setModel(listModel);
				listMain.setSelectedIndex(0);
				lblList.setText("Spare Parts: ");
			}
			if (e.getSource() == btnRepair) {
				listModel.clear();
				for (Repair r : service.returnRepairs()) {
					listModel.addElement(r);
				}
				listMain.setModel(listModel);
				listMain.setSelectedIndex(0);
				listMain.setModel(listModel);
				listMain.setSelectedIndex(0);
				lblList.setText("Repairs: ");
			}
			if (e.getSource() == btnOrder) {
				listModel.clear();
				for (Order order : service.returnOrders()) {
					listModel.addElement(order);
				}
				listMain.setModel(listModel);
				listMain.setSelectedIndex(0);
				lblList.setText("Orders: ");
			}
			if (e.getSource() == btnDrawer) {
				listModel.clear();
				for (StorageArea drawer : service.returnDrawers()) {
					listModel.addElement(drawer);
				}
				listMain.setModel(listModel);
				listMain.setSelectedIndex(0);
				lblList.setText("Drawers: ");
			}
			if(e.getSource() == mnuItem){
				System.exit(0);
			}
		}
	}

	public class ListController implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (e.getSource() == listMain) {
				if (listMain.getSelectedValue() instanceof Machine) {
					Machine m = (Machine) listMain.getSelectedValue();
					txfInfo1.setText(" Model: " + m.getModel());
					txfInfo2.setText(" Manufacturer: " + m.getManufacturer());
					txfInfo3.setText(" Manual: " + m.getManualLocation());
					txfInfo4.setText(" Sketch: " + m.getSketchNumber());
					txfInfo5.setText(" List of repairs:");
					scpSub.setVisible(true);
					pnlCenterRightDown.add(scpSub2);
					scpSub2.setVisible(true);
					listSub.setListData(m.getRepairs().toArray());
					listSub2.setListData(m.returnParts().toArray());
				} 
				else if (listMain.getSelectedValue() instanceof Repair) {
					Repair r = (Repair) listMain.getSelectedValue();
					txfInfo1.setText(" Machine: " + r.getMachine().getCode());
					txfInfo2.setText(" Start Date: "
							+ r.getDate(r.getStartDate()));
					txfInfo3.setText(" End Date: " + r.getDate(r.getEndDate()));
					txfInfo4.setText("");
					txfInfo5.setText(" Parts used: ");
					scpSub.setVisible(true);
					scpSub2.setVisible(false);
					pnlCenterRightDown.remove(scpSub2);
					listSub.setListData(r.getRepairLines().toArray());
				}
				else if (listMain.getSelectedValue() instanceof SparePart) {
					SparePart sp = (SparePart) listMain.getSelectedValue();
					txfInfo1.setText(" Location: " + sp.getDrawer());
					txfInfo2.setText(" Drawing: " + sp.getSketch());
					txfInfo3.setText(" Available in stock: " + sp.getAmount());
					txfInfo4.setText(" Mininum required: "
							+ sp.getMinimumAmount());
					txfInfo5.setText("");
					scpSub.setVisible(false);
					scpSub2.setVisible(false);
				}
				else if (listMain.getSelectedValue() instanceof StorageArea) {
					StorageArea sa = (StorageArea) listMain.getSelectedValue();
					txfInfo1.setText(" ID: " + sa.getId());
					txfInfo2.setText("");
					txfInfo3.setText("");
					txfInfo4.setText("");
					txfInfo5.setText("");
					scpSub.setVisible(false);
					scpSub2.setVisible(false);
				}
				else if (listMain.getSelectedValue() instanceof Order) {
					Order o = (Order) listMain.getSelectedValue();
					txfInfo1.setText(" Title: " + o.getTitle());
					txfInfo2.setText(" Issued on: " + o.getDate());
					txfInfo3.setText("");
					txfInfo4.setText("");
					txfInfo5.setText(" Contains: ");
					scpSub.setVisible(true);
					scpSub2.setVisible(false);
					pnlCenterRightDown.remove(scpSub2);
					listSub.setListData(o.getOrderLines().toArray());
				}
				
			}

		}

	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			System.out
					.println("Error setting look and feel: " + e.getMessage());
		}
		GuiDesignPlan frame = new GuiDesignPlan();
		frame.setVisible(true);
	}

}
